package com.admore.demo.screen;

import static com.admore.sdk.config.AdMoreConstants.AD_HEIGHT;
import static com.admore.sdk.config.AdMoreConstants.AD_WIDTH;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.admore.demo.App;
import com.admore.demo.R;
import com.admore.demo.util.UIUtils;
import com.admore.sdk.ads.AdMoreInterstitialAd;
import com.admore.sdk.callback.AdMoreLoadListener;
import com.admore.sdk.callback.AdMoreMediaListener;
import com.admore.sdk.sdk.AdMoreSdk;
import com.zcy.base.ui.BaseActivity;

import java.util.HashMap;

public class FullScreenActivity extends BaseActivity {

    private ViewGroup adMain;
    private AdMoreInterstitialAd interstitialAd;

    @Override
    public int bindLayout() {
        return R.layout.activity_screen;
    }

    @Override
    public void initView() {
        adMain = findViewById(R.id.ad_main);
        ((TextView) findViewById(R.id.title)).setText("full Screen");
        ((TextView) findViewById(R.id.title)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (interstitialAd == null) {
                    startAd();
                } else {
                    if (interstitialAd.isReady()) {
                        showAd();
                    } else {
                        interstitialAd.loadAd();
                    }
                }
            }
        });
    }

    @Override
    public void initData() {
        startAd();
    }

    private void startAd() {
        if (!AdMoreSdk.init) {
            return;
        }
        HashMap<String, Object> options = new HashMap<>();
        options.put(AD_WIDTH, UIUtils.getScreenWidth(getApplicationContext()));
//        options.put(AD_HEIGHT, UIUtils.getScreenHeight(getApplicationContext()));
        interstitialAd = AdMoreSdk.getAdMoreSdkManager().createInterstitialAd(this, App.AD_MORE_INTERSTITIAL_ID, options);
        interstitialAd.setLoadListener(new AdMoreLoadListener() {
            @Override
            public void onAdLoadSuccess(String msg) {
                Log.d(TAG, "onAdLoadSuccess");
                showAd();
            }

            @Override
            public void onAdLoadError(Throwable throwable) {
                Log.d(TAG, "onAdLoadError " + throwable);
            }
        });
        interstitialAd.loadAd();
    }


    private void showAd() {

        if (interstitialAd.isReady()) {
            interstitialAd.setMediaListener(new AdMoreMediaListener() {
                @Override
                public void onAdClosed() {
                    Log.d(TAG, "onAdClosed");
                }

                @Override
                public void onAdShow() {
                    Log.d(TAG, "onAdShow ecpm:" + interstitialAd);
                }
            });
            interstitialAd.show(this, null);
        } else {
            Toast.makeText(getApplicationContext(), "请先加在广告", Toast.LENGTH_SHORT).show();
        }
    }

}
